package com.tgy.leetcode;



import java.util.*;

/**
 * @Author: tgy
 * @Date: 2021/1/11 下午8:59
 */
public class 字符串的种类 {


    /**
     * 只包含小写字母
     * @param strs
     * @return
     */
    public int classifyOfStr(String[] strs) {

        Set<Integer> sets = new HashSet<>();

        for (String str : strs) {

            int flag = 0;
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {

                int index = chars[i] - 'a';
                flag |= (1 << index);
            }

            sets.add(flag);
        }

        return sets.size();
    }

    public int classifyOfStr01(String[] strs) {


        Set<Set<Character>> sets = new HashSet<>();


//        Set<Character> set =

        for (String str : strs) {

            HashSet<Character> hashSet = new HashSet<>();

            for (char c : str.toCharArray()) {
                hashSet.add(c);
            }
            sets.add(hashSet);
        }

        return sets.size();
    }


   private List<String> randomStr(int minLen, int maxLen,int count) {

        List<String> result = new ArrayList<>();
        if (count <= 0) {

            return result;
        }

        Random random = new Random();

        int sep = maxLen - minLen;

        for (int i = 0; i < count; i++) {

            int len = minLen + random.nextInt(sep);
            result.add(generalStr(len));
        }

        return result;
    }

    private String generalStr(int len) {

        StringBuilder sb = new StringBuilder();

        Random random = new Random();
        for (int i = 0; i < len; i++) {

            sb.append((char)('a' + random.nextInt(26)));
        }

        return sb.toString();
    }

    private static void printTime(Runnable runnable) {


        long start = System.currentTimeMillis();
        runnable.run();
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
    }

    public static void main(String[] args) {

        字符串的种类 classifyOfStr = new 字符串的种类();

        List<String> strings = classifyOfStr.randomStr(1, 10, 1000000);

//        for (String string : strings) {
//
//            System.out.println(string);
//        }

        String[] strs = strings.toArray(new String[0]);

        new Thread(()->{

            printTime(()->{

                System.out.println(classifyOfStr.classifyOfStr01(strs));
            });

            System.out.println(Thread.currentThread().getName());
        },"t1").start();

        new Thread(()->{

            printTime(()->{

                System.out.println(classifyOfStr.classifyOfStr(strs));
            });

            System.out.println(Thread.currentThread().getName());
        },"t2").start();

    }
}
